package com.gitee.wsl.unit.angle

import com.gitee.wsl.unit.angle.ext.sanitize360

/**
 * Defines 4 quadrants of a circle, and the angle range that each occupies, in degrees going
 * clockwise.
 */
@Suppress("MagicNumber")
enum class Quadrant(val angleRange: OpenEndRange<Float>) {

    SouthEast(0f..<90f),
    SouthWest(90f..<180f),
    NorthWest(180f..<270f),
    NorthEast(270f ..< 360f);

    companion object{

        //得到第一 第二 第三 第四象限，以圆心坐标为原点计算，数学公式
        fun touchOnWhichPart(centerX: Float, centerY: Float, x: Float, y: Float): Quadrant {
            return if (x > centerX) {
                       if (y > centerY) NorthEast else SouthEast
                   } else
                       if (y > centerY) NorthWest else SouthWest
        }

        fun from(angle: AngleUnit) = angle.quadrant
    }
}

fun Quadrant.isWest(): Boolean {
    return this == Quadrant.NorthWest || this == Quadrant.SouthWest
}

fun Quadrant.isSouth(): Boolean {
    return this == Quadrant.SouthWest || this == Quadrant.SouthEast
}

fun Quadrant.isNorth(): Boolean {
    return this == Quadrant.NorthWest || this == Quadrant.NorthEast
}

val AngleUnit.quadrant:Quadrant
    get() = when (sanitize360().degrees.toFloat()) {
        in Quadrant.SouthWest.angleRange -> Quadrant.SouthWest
        in Quadrant.NorthWest.angleRange -> Quadrant.NorthWest
        in Quadrant.NorthEast.angleRange -> Quadrant.NorthEast
        else -> Quadrant.SouthEast
    }